/*
 * SidoraTools.java
 *
 * Created on 23 de agosto de 2007, 19:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.sidora.strata;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import org.sidora.core.Category;
import org.sidora.core.Describable;
import org.sidora.core.Describer;
import org.sidora.core.Site;
import org.sidora.core.context.Context;
import org.sidora.core.util.AbstractInventory;

/**
 * SidoraTools
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
public class SidoraTools {
    
    /**
     * Creates a new instance of SidoraTools
     */
    public SidoraTools() {
    }
    
    //Genera e inicialitza els desplegables per a seleccionar les categories de un determinat atribut/camp.
    public static JPopupMenu generatePopupMenuCategory(final Describable describable, final Field atribute, final JTextField jTextField){
        JPopupMenu jPopupMenu = new JPopupMenu();
        Site site = describable.getSite();
        List<Describer> describers = Describer.Inventory.getList(site);
        //TreeSet<Describer> types = new TreeSet<Describer>(describers);
        Iterator <Describer>iter = describers.iterator();

        //TreeSet<Describer> types = new TreeSet<Describer>(Describer.Inventory.getList(describable.getSite()));
        
        //Iterator <Describer>iter = types.iterator();
        
        while (iter.hasNext()){
            Describer atype = iter.next();
            
            if (atype.getField().getName().equals(atribute.getName())){
                if (atype.getReferencedClass().isInstance(describable)){
                    
                    JMenu ajMenu = new JMenu();
                    jPopupMenu.add(ajMenu);
                    ajMenu.setText(atype.getLabel());
                    Iterator<Category> iter2 = Category.Inventory.getList(atype).iterator();
                    
                    while (iter2.hasNext()){
                        final Category acat = iter2.next();
                        final JMenuItem item = new JMenuItem();
                        item.setText(acat.getValue());
                        ajMenu.add(item);
                        item.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent arg0) {
                                jTextField.setText(item.getText());
                                Class classC = describable.getClass();
                                Field field;
                                try {
                                    //field = classC.getField(atribute.getName());
                                    //field.set(describable,acat);
                                    atribute.set(describable,acat);
                                } catch (SecurityException ex) {
                                    ex.printStackTrace();
//                                } catch (NoSuchFieldException ex) {
//                                    ex.printStackTrace();
                                } catch (IllegalArgumentException ex) {
                                    ex.printStackTrace();
                                } catch (IllegalAccessException ex) {
                                    ex.printStackTrace();
                                }
                                //context.setTipusint(acat);
                                AbstractInventory.setObject(describable);
                            }
                        });
                    }
                }
            }
        }
        return jPopupMenu;
    }
    
    
    public static JPopupMenu generatePopupMenuCategorys(final Describable describable, final Field atribute, final JTextField jTextField){
        JPopupMenu jPopupMenu = new JPopupMenu();
        TreeSet<Describer> types = new TreeSet<Describer>(Describer.Inventory.getList(describable.getSite()));
        Iterator <Describer>iter = types.iterator();
        
        while (iter.hasNext()){
            Describer atype = iter.next();
            
            if (atype.getField().getName().equals(atribute.getName())){
                if (atype.getReferencedClass().isInstance(describable)){
                    
                    JMenu ajMenu = new JMenu();
                    jPopupMenu.add(ajMenu);
                    ajMenu.setText(atype.getLabel());
                    Iterator<Category> iter2 = Category.Inventory.getList(atype).iterator();
                    TreeSet<Category> elementos = new TreeSet();
                    
                    try{
                        elementos = (TreeSet) atribute.get(describable);
                    } catch (SecurityException ex) {
                        ex.printStackTrace();
                    } catch (IllegalArgumentException ex) {
                        ex.printStackTrace();
                    } catch (IllegalAccessException ex) {
                        ex.printStackTrace();
                    }
                    
                    while (iter2.hasNext()){
                        final Category acat = iter2.next();
                        final JCheckBoxMenuItem item = new JCheckBoxMenuItem();
                        item.setText(acat.getValue());
                        if (elementos.contains(acat)){
                            item.setSelected(true);
                        }
                        
                        ajMenu.add(item);
                        item.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent arg0) {
                                Class classC = describable.getClass();
                                Field field;
                                try {
                                    //atribute.set(describable,acat);
                                    TreeSet<Category>destructionTraces = (TreeSet)atribute.get(describable);
                                    
                                    if (item.getState()){
                                        destructionTraces.add(acat);
                                    }else{
                                        destructionTraces.remove(acat);
                                    }
                                    
                                    atribute.set(describable, destructionTraces);
                                    
                                    jTextField.setText(TreeSet2String(destructionTraces));
                                    
                                } catch (SecurityException ex) {
                                    ex.printStackTrace();
                                } catch (IllegalArgumentException ex) {
                                    ex.printStackTrace();
                                } catch (IllegalAccessException ex) {
                                    ex.printStackTrace();
                                }
                                AbstractInventory.setObject(describable);
                            }
                        });
                    }
                }
            }
        }
        return jPopupMenu;
    }
    
    
    public static String TreeSet2String(TreeSet set) {
        StringBuffer textbuffer = new StringBuffer("[");
        boolean empty = set.isEmpty();
        Iterator iter = set.iterator();
        
        while (iter.hasNext()){
            Category cat = (Category) iter.next();
            textbuffer.append(cat.toString());
            
            if (iter.hasNext())
                textbuffer.append("/");
        }
        textbuffer.append("]");
        
        return textbuffer.toString();
    }
    
    public static void showElement(){
            Storage.getSectorAction().cerrar();
            Storage.getFeatureAction().cerrar();
            Storage.getContextFormAction().cerrar();
            Storage.getSiteFormAction().cerrar();
            Storage.getCategoryFormAction().cerrar();
    }
    
}
